package com.hh.base.ui

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.hh.base.ui.bridge.Event
import com.hh.base.ui.bridge.Penta
import com.hh.base.widget.dialog.MultifunctionDialog
import com.hh.base.widget.pickerview.listener.OnTimeSelectListener
import java.util.Date

/**
 * Create By hHui on 2025/4/3 14:00
 *
 * @description
 */
abstract class BaseViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel(), DefaultLifecycleObserver, BaseView {
    val TAG = this.javaClass.simpleName
    private val _toastMessage = MutableLiveData<Event<String>>()
    val toastMessage: LiveData<Event<String>> = _toastMessage

    private val _alertMessage = MutableLiveData<Event<String>>()
    val alertMessage: LiveData<Event<String>> = _alertMessage

    private val _alertTitleAndMessage = MutableLiveData<Event<Pair<String, String>>>()
    val alertTitleAndMessage: LiveData<Event<Pair<String, String>>> = _alertTitleAndMessage

    private val _loadingMessage = MutableLiveData<Event<String>>()
    val loadingMessage: LiveData<Event<String>> = _loadingMessage

    private val _hideLoading = MutableLiveData<Event<Unit>>()
    val hideLoading: LiveData<Event<Unit>> = _hideLoading


    private val _loadingCancel = MutableLiveData<Event<Triple<String, String, MultifunctionDialog.AlertCancelListener>>>()
    val loadingCancel: LiveData<Event<Triple<String, String, MultifunctionDialog.AlertCancelListener>>> = _loadingCancel

    private val _alertOption = MutableLiveData<Event<Penta<String, String, MultifunctionDialog.AlertClickListener, String, String>>>()
    val alertOption: LiveData<Event<Penta<String, String, MultifunctionDialog.AlertClickListener, String, String>>> = _alertOption

    private val _jump2Activity = MutableLiveData<Event<Triple<Class<*>, Boolean, Map<String, String>?>>>()
    val jump2Activity: LiveData<Event<Triple<Class<*>, Boolean, Map<String, String>?>>> = _jump2Activity
    private val _showDateTimePicker = MutableLiveData<Event<Triple<Date?, OnTimeSelectListener, BooleanArray>>>()
    val showDateTimePicker: LiveData<Event<Triple<Date?, OnTimeSelectListener, BooleanArray>>> = _showDateTimePicker


    override fun showToast(msg: String) {
        _toastMessage.value = Event(msg)
    }

    override fun alertMessage(msg: String) {
        _alertMessage.value = Event(msg)
    }

    override fun alertTitleAndMessage(title: String, msg: String) {
        _alertTitleAndMessage.value = Event(Pair(title, msg))
    }

    override fun showLoading(msg: String) {
        _loadingMessage.value = Event(msg)
    }

    override fun showLoadingCancel(msg: String, cancelText: String, cancelListener: MultifunctionDialog.AlertCancelListener) {
        _loadingCancel.value = Event(Triple(msg, cancelText, cancelListener))
    }

    override fun hideLoading() {
        _hideLoading.value = Event(Unit)
    }

    override fun alertOption(title: String, msg: String, clickListener: MultifunctionDialog.AlertClickListener, sureText: String, cancelText: String) {
        _alertOption.value = Event(Penta(title, msg, clickListener, sureText, cancelText))
    }

    override fun jump2Activity(cla: Class<*>, isFinishBefore: Boolean, map: Map<String, String>?) {
        _jump2Activity.value = Event(Triple(cla, isFinishBefore, map))
    }

    override fun showDateTimerPicker(callback: OnTimeSelectListener, date: Date?, array: BooleanArray) {
        _showDateTimePicker.value = Event(Triple(date, callback, array))
    }


    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        initData()
    }

    fun getStringExtra(key: String): String? {
        return savedStateHandle.get<String>(key)
    }

    protected abstract fun initData()
}